package main;

import java.io.File;

import utils.BlockUtils;

public class Main {
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if(args.length != 3) {
			System.out.println("This command need 3 args");
			System.exit(1);
		}
		String root = args[0];
		String src = args[1];
		String dest = args[2];
		root = BlockUtils.prepareRoot(root);
		src = BlockUtils.prepareDest(src, root);
		dest = BlockUtils.prepareDest(dest, root);
		File file1 = BlockUtils.checkDestFile(root, src);
		if(file1 == null) {
			System.out.println("File doesn't exists in dir " + src);
			System.exit(1);
		}
		Integer extractBlockNumber = BlockUtils.extractBlockNumber(file1);
		if(extractBlockNumber == null)
			System.exit(1);
		File file2 = BlockUtils.checkDestToCatalogue(root, src);
		if(file2 == null) {
			System.out.println("Catalogue doesn't exists in dir " + src);
			System.exit(1);
		}
		File file3 = BlockUtils.checkDestFile(root, dest);
		if(file3 == null) {
			System.out.println("Catalogue doesn't exists in dir " + src);
			System.exit(1);
		}
		move(file2, file3, extractBlockNumber, root);
	}
	private static void move(File file1, File file2, Integer number, String root) {
		BlockUtils.removeCataloguePointer(file1, number, root);
		BlockUtils.addCataloguePointer(file2, number, root);
	}
	
}
